<?php
class Aalbum {
	const MAXNUM  = 50;
	public static function main($funcode) {
		switch ($funcode) {
			case 1:
				sview::verify(true, true);
				self::__dosave();
				break;
			case 2:
				sview::verify(true, true);
				self::__dodelete();
				break;
			case 3:
				sview::verify(true, true);
				self::__doprize();
				break;
			case 4:
				sview::verify();
				self::__dolist();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//上传相册图片3-4-1
	private static function __dosave() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		//验证是主播
		$anchor= model::load('live/anchor')->get_one($userid);
		if (!$anchor) {
			sview::$view->assign_error(80009, lang::text(80009));
		}
		//验证上限
		$field = array('userid'=>$userid);
		$total = model::load('live/photo')->set_devision($userid)->get_total($field);
		if ($total >= self::MAXNUM) {
			sview::$view->assign_error(60024, lang::replace(60024, self::MAXNUM));
		}
		//验证备注
		$note  = trim(sview::$ctrl['note']);
		if (mb_strlen($note, 'utf-8') > 100) {
			sview::$view->assign_error(60004, lang::text(60004));
		}
		$note  = htmlspecialchars($note);
		$src   = trim(sview::$ctrl['src']);
		if (!$src) {
			sview::$view->assign_error(60003, lang::text(60003));
		}
		$asrc  = explode(',', $src);
		$list  = array();
		foreach ($asrc as $src) {
			$src   = htmlspecialchars($src);
			$field = array('userid'=>$userid, 'src'=>$src, 'note'=>$note, 'stime'=>config::$nowtime);
			$pid   = model::load('live/photo')->set_devision($userid)->new_one($field);
			$smsrc = str_replace('.', '_sm.', $src);
			$list[]= array('id'=>$pid, 'src'=>$src, 'smsrc'=>$smsrc);
		}
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
	
	//删除相册图片3-4-2
	private static function __dodelete() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$pid    = intval(sview::$ctrl['id']);
		$porm   = model::load('live/photo')->set_devision($userid);
		$photo  = $porm->get_one($pid);
		if ($photo['userid'] == $userid) {
			upfile::delete($photo['src']);
			upfile::delete(str_replace('.', '_sm.', $photo['src']));
			$porm->delete($pid);
			//删除点赞相关的资料信息
			model::load('live/photoprise')->set_devision($photo['anchorid'])->get_orm()
				->where('photoid', $pid)->delete();
		}
		sview::$view->display();
	}
	
	//点赞相册图片3-4-3
	private static function __doprize() {
		$acl      = acl::getinst('vedio');
		$userid   = $acl->user['id'];
		$id       = (int)sview::$ctrl['id'];
		$anchorid = (int)sview::$ctrl['anchorid'];
		$status   = (int)sview::$ctrl['status'];
		$porm     = model::load('live/photoprise')->set_devision($anchorid);
		$field    = array('photoid'=>$id, 'anchorid'=>$anchorid, 'userid'=>$userid);
		$item     = $porm->get_item($field);
		if ($status && !$item) { //点赞记录信息
			$porm->new_one($field);
			$field = array(array('name'=>'prise', 'value'=>'`prise`+1', 'type'=>orm::DT_SQL));
			model::load('live/photo')->set_devision($anchorid)->save($id, $field);
		} else if (!$status && $item) { //取消点赞
			$porm->delete($item['id']);
			$field = array(array('name'=>'prise', 'value'=>'`prise`-1', 'type'=>orm::DT_SQL));
			model::load('live/photo')->set_devision($anchorid)->save($id, $field);
		}
		sview::$view->display();
	}
	
	//主播相册列表3-4-4
	private static function __dolist() {
		$userid = (int)sview::$ctrl['userid'];
		$start  = (int)sview::$ctrl['offset'];
		$limit  = (int)sview::$ctrl['limit'];
		$where  = array('userid'=>$userid);
		$fields = 'id,src,note,prise,stime';
		$list   = model::load('live/photo')->set_devision($userid)
			->get_only_list($start, $limit, $where, $fields, 'id', orm::DESC);
		if ($list) {
			$acl  = acl::getinst('vedio');
			$pids = array();
			//获取我点赞的相册列表信息
			if ($acl->user) {
				$uid  = $acl->user['id'];
				$where= array('anchorid'=>$userid, 'userid'=>$uid);
				$pids = model::load('live/photoprise')->set_devision($userid)
					->get_only_column(0, -1, $where, 'photoid');
			}
			foreach ($list as &$item) {
				$item['isprise'] = (empty($pids) || !in_array($item['id'], $pids))? 0:1;
				$item['smsrc']   = str_replace('.', '_sm.', $item['src']);
			}
		}
		$total = model::load('live/photo')->set_devision($userid)
			->get_total(array('userid'=>$userid));
		$data  = array('userid'=>$userid, 'lastnums'=>self::MAXNUM - $total, 'list'=>$list);
		sview::$view->assign_by_ref('datasets', $data);
		sview::$view->display();
	}
}